﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace Gmantis.Controls.DataGrid
{
    public class DataGridSortStateTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType.FullName == "System.String") || base.CanConvertFrom(sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            int num2;
            if (!(value is string))
            {
                return base.ConvertFrom(context, culture, value);
            }
            string[] strArray = (value as string).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            DataGridSortDirection none = DataGridSortDirection.None;
            int num = -1;
            if (strArray.Length > 0)
            {
                try
                {
                    none = (DataGridSortDirection)Enum.Parse(typeof(DataGridSortDirection), strArray[0], true);
                }
                catch
                {
                }
            }
            if ((strArray.Length > 1) && int.TryParse(strArray[1], out num2))
            {
                num = num2;
            }
            return new DataGridSortState(none, new int?(num));
        }
    }
}
